package org.yxing.palpalgps;


import java.util.LinkedList;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class ActionBar extends LinearLayout {
    
    private LinkedList<ImageButton> items;
    
    private OnActionBarListener mOnActionBarListener;
    
    public enum Type {Search,Refresh,Edit,Add,Settings,List,Eye,Trashcan, Chat, Photo, Group};

    public ActionBar(Context context) {
        this(context, null);
    }
    
    public ActionBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        items = new LinkedList<ImageButton>();
        
        LayoutInflater.from(context).inflate(R.layout.action_bar, this);
    }
    
    public void onFinishInflate() {
        super.onFinishInflate();
    }
    
    public void setOnActionBarListener(OnActionBarListener listener) {
        mOnActionBarListener = listener;
    }
    
    
    public void addItem(int resid) {
        ImageButton item = createItem(resid);
        addItem(item);
    }
    
    public void addItem(Type type) {
        
        ImageButton item = createItemByType(type);
        addItem(item);
    }
    
    public void addItem(ImageButton item) {
        addView(item);
        items.add(item);
    }
    
    public void clear() {
        for (View v : items) {
            removeView(v);
        }
//        setOnActionBarListener(null);
        items.clear();
    }
    
    public ImageButton createItemByType(Type type) {
        int resId;
        switch (type) {
        case Eye:
            resId = R.drawable.action_bar_eye;
            break;
        case Edit:
            resId = R.drawable.action_bar_edit;
            break;
        case Refresh:
            resId = R.drawable.action_bar_refresh;
            break;
        case Trashcan:
            resId = R.drawable.action_bar_trashcan;
            break;
        case Add:
            resId = R.drawable.action_bar_add;
            break;
        case Chat:
            resId = R.drawable.action_bar_talk;
            break;
        case Photo:
            resId = R.drawable.action_bar_take_photo;
            break;
        case Group:
            resId = R.drawable.action_bar_group;
            break;
        default:
            return null;
        }
        
        return createItem(resId);
    }
    
    public ImageButton createItem(int resid) {
        ImageButton item = (ImageButton)LayoutInflater.from(getContext()).inflate(R.layout.action_bar_item, null);
        
        int width = (int) getResources().getDimension(R.dimen.action_bar_height);
        LayoutParams lr = new LayoutParams(width, LayoutParams.FILL_PARENT);
        
        item.setLayoutParams(lr);
        
        item.setImageDrawable(getResources().getDrawable(resid));
        item.setOnClickListener(mOnClickListener);
        return item;
    }

    public interface OnActionBarListener {
        public void onActionBarItemClick(int position);
    }
    
    private OnClickListener mOnClickListener = new OnClickListener() {
        public void onClick(View v) {
            if (mOnActionBarListener != null) {
                int count = items.size();
                for (int i = 0; i < count; i ++) {
                    if (v == items.get(i)) {
                        mOnActionBarListener.onActionBarItemClick(i);
                        break;
                    }
                }
            }
        }
    };
    
}
